﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AR.Drone.Data.Navigation;
using AvionicsInstrumentControlDemo;

namespace isDrone
{
    public partial class UiControl : UserControl
    {   
        InstrumentControl _control;
        private int _type;
        private Telemetry _telemetry;
        /* 0 - пусто
         * 1 - высотомер
         * 2 - горизонт
         * 3 - компас
         */
        public UiControl()
        {
            InitializeComponent();
        }

        public Telemetry Telemitry
        {
            set
            {
                _telemetry = value;
                _telemetry.NewTelemetry += Cheing;
            }
        }
        delegate void CheingCallback();
        public void Cheing()
        {

            if (_control.InvokeRequired)
            {
                CheingCallback d = Cheing;
                Invoke(d);
            }
            else
            {
                var date = _telemetry.Date;
                if (checkBox1.Checked)
                {
                    if (_type == 1)
                    {
                        ((AltimeterInstrumentControl) _control).SetAlimeterParameters(Convert.ToInt32(date.Altitude*100));
                    }
                    if (_type == 2)
                    {
                        ((AttitudeIndicatorInstrumentControl) _control).SetAttitudeIndicatorParameters(
                            date.Pitch/Math.PI*180, -date.Roll/Math.PI*180);
                    }
                    if (_type == 3)
                    {
                        ((HeadingIndicatorInstrumentControl) _control).SetHeadingIndicatorParameters(date.Yaw >= 0
                            ? Convert.ToInt32(date.Yaw/Math.PI*180)
                            : 360 + Convert.ToInt32(date.Yaw/Math.PI*180));
                    }
                }
            }

        }

        public int Type
        {
            get { return _type; }
            set
            {
                if (_type != 0)
                    Controls.Clear();
                _type = value;
                if (_type == 1)
                    _control = new AltimeterInstrumentControl();
                if (_type == 2)
                    _control = new AttitudeIndicatorInstrumentControl();
                if (_type == 3)
                    _control = new HeadingIndicatorInstrumentControl();
                if (_control != null)
                {
                    _control.Dock = DockStyle.Fill;
                    if (_control != null) _control.Parent = this;
                }
            }
        }

        public double TangagKren
        {
            get
            {
                return _type == 2 ? trackBar1.Value/100.0 : -1;
            }
        }

        public double Riskanie
        {
            get
            {
                return _type == 3 ? trackBar1.Value / 100.0 : -1;
            }
        }


        public double Altitude
        {
            get
            {
                return _type == 1 ? trackBar1.Value / 100.0 : -1;
            }
        }

        public event SettingHandler SettingEvente;

        public delegate void SettingHandler(UiControl c);

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (SettingEvente != null) SettingEvente(this);
        }

        private int lh;
        private int lt;
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == false)
            {
                lt = Top;
                lh = Height;
                Height = checkBox1.Height + 5;
                Top = Top + Height - checkBox1.Height + 5;
            }
            else
            {
                Height = lh;
                Top = lt;
            }
        }
    }
}
